package com.maxd.controller.wph;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.maxd.adapter.IgnoreSecurity;
import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.model.CommonInfo;
import com.maxd.model.Req;
import com.maxd.model.UserInfo;
import com.maxd.respository.common.CommonRepository;
import com.maxd.respository.userinfo.UserJpaRepository;
import com.maxd.respository.userinfo.UserRepository;
import com.maxd.utils.Config;
import com.maxd.utils.HttpClientUtil;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.*;
import com.taobao.api.response.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@Api(value = "唯品会api管理", tags = {"唯品会api管理"})
@Slf4j
@RequestMapping(value = "/wph")
public class WphController {

    @Autowired
    private CommonRepository commonRepository;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private UserJpaRepository userJpaRepository;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation("全网淘客商品查询API")
    @IgnoreSecurity
    @ResponseBody
    public Result list(int page,int size,
                       @ApiParam("排序 1排序字段: COMMISSION-佣金，PRICE-价格,COMM_RATIO-佣金比例，DISCOUNT-折扣") String sort,
                       @ApiParam("搜索商品名称")String keyword) {
        CommonInfo appkey = commonRepository.findOne(30);
        CommonInfo key = commonRepository.findOne(239);
        Map<String,String> map=new HashMap<>();
        map.put("apkey",appkey.getValue());
        map.put("key",key.getValue());
        map.put("pageSize",String.valueOf(size));
        map.put("page",String.valueOf(page));
        map.put("fieldName",sort);
        map.put("keyword",keyword);
        map.put("chanTag","default_pid");
        map.put("openId","default_open_id");
        map.put("realCall","true");
        String body = HttpClientUtil.doGet("http://api.web.ecapi.cn/vip/getUnionGoodsQuery", map);
        return ResultUtil.success(JSONObject.parseObject(body));
    }



    @RequestMapping(value = "/details", method = RequestMethod.GET)
    @ApiOperation("获取商品详情")
    @ResponseBody
    public Result details(@ApiParam("宝贝ID") String itemid) {
        CommonInfo appkey = commonRepository.findOne(30);
        CommonInfo key = commonRepository.findOne(239);
        Map<String,String> map=new HashMap<>();
        map.put("apkey",appkey.getValue());
        map.put("key",key.getValue());
        map.put("goodsIdList",itemid);
        map.put("chanTag","default_pid");
        map.put("openId","default_open_id");
        map.put("realCall","true");
        String body = HttpClientUtil.doGet("http://api.web.ecapi.cn/vip/getGoodsInfo", map);
        JSONObject jsonObject = JSONObject.parseObject(body);
        String code = jsonObject.getString("code");
        if("200".equals(code)){
            JSONArray data = jsonObject.getJSONArray("data");
            if(data.size()>0){
                JSONObject jsonObject1 = data.getJSONObject(0);
                String goodsMainPicture = jsonObject1.getString("goodsMainPicture");
                JSONArray jsonArray=new JSONArray();
                jsonArray.add(goodsMainPicture);
                jsonObject1.put("goodsCarouselPictures",jsonArray);
            }
        }
        return ResultUtil.success(jsonObject);
    }

    @RequestMapping(value = "/getLink", method = RequestMethod.GET)
    @ApiOperation("转链")
    @ResponseBody
    public Result getLink(@ApiParam("商品id") String itemid,String relationId) {
        CommonInfo appkey = commonRepository.findOne(30);
        CommonInfo key = commonRepository.findOne(239);
        UserInfo userByRelationId = userRepository.getUserByRelationId(relationId);
        if(StringUtils.isEmpty(userByRelationId.getWhp())){
            Map<String,String> maps=new HashMap<>();
            maps.put("apkey",appkey.getValue());
            maps.put("key",key.getValue());
            maps.put("pidNameList",userByRelationId.getRelationId());
            String body = HttpClientUtil.doGet("http://api.web.ecapi.cn/vip/createPid", maps);
            JSONObject jsonObject = JSONObject.parseObject(body);
            JSONObject data = jsonObject.getJSONObject("data");
            JSONArray pidInfoList = data.getJSONArray("pidInfoList");
            String pid = pidInfoList.getJSONObject(0).getString("pid");
            userByRelationId.setWhp(pid);
            userJpaRepository.save(userByRelationId);
        }

        Map<String,String> map=new HashMap<>();
        map.put("apkey",appkey.getValue());
        map.put("key",key.getValue());
        map.put("goodsIdList",itemid);
        map.put("chanTag",userByRelationId.getRelationId());
        map.put("openId","default_open_id");
        map.put("realCall","true");
        map.put("adCode","oldversion");
        String body = HttpClientUtil.doGet("http://api.web.ecapi.cn/vip/generalLinkById", map);
        return ResultUtil.success(JSONObject.parseObject(body));
    }


    @RequestMapping(value = "/dyAnalyzeCode", method = RequestMethod.GET)
    @ApiOperation("口令解析")
    @ResponseBody
    public Result dyAnalyzeCode(@ApiParam("口令/链接等内容") String content) {
        CommonInfo appkey = commonRepository.findOne(30);
        CommonInfo key = commonRepository.findOne(239);
        Map<String,String> map=new HashMap<>();
        map.put("apkey",appkey.getValue());
        map.put("key",key.getValue());
        map.put("source",Config.BASE_NAME);
        map.put("content",content);
        String body = HttpClientUtil.doGet("http://api.web.ecapi.cn/vip/changeCpsUrlToItem", map);
        return ResultUtil.success(JSONObject.parseObject(body));
    }





}
